package com.springboot.framework.controller;

import com.springboot.framework.annotation.ACS;
import com.springboot.framework.controller.request.ProductInsertBean;
import com.springboot.framework.controller.request.StatusUpdateBean;
import com.springboot.framework.controller.request.ProductUpdateBean;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.model.UserModel;
import com.springboot.framework.vo.ResponseVO;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.ProductModel;
import com.springboot.framework.service.ProductService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author SWF
 * @date 2019/6/6 15:09
 **/
@Api(description = "商品")
@RestController
@RequestMapping("/product")
public class BuyerProductController extends BaseController{

    @Resource
    private ProductService productService;

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "所有已上架商品",notes = "所有已上架商品")
    @GetMapping("/list")
    public ResponseVO listUp(@RequestParam Integer shopId){
        return ResponseVoUtil.success(convertProductVOFromProductModel(productService.listUp(shopId)));
    }

    private ProductVO convertProductVOFromProductModel(ProductModel productModel){
        if(productModel == null){
            return null;
        }
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(productModel,productVO);
        return productVO;
    }

    private List<ProductVO> convertProductVOFromProductModel(List<ProductModel> productModels){
        if(productModels == null){
            return null;
        }
        return productModels.stream()
                .map(e->convertProductVOFromProductModel(e))
                .collect(Collectors.toList());
    }

}
